
package com.xuelangyun.form.exterauth.service.impl;

import java.util.Date;

import com.xuelangyun.form.api.exterauth.form.LogForm;
import com.xuelangyun.form.common.utils.PageUtils;
import com.xuelangyun.form.common.utils.Query;
import com.xuelangyun.form.exterauth.dao.ISysExterLogDao;
import com.xuelangyun.form.exterauth.entity.SysExterLogApiEntity;
import com.xuelangyun.form.exterauth.service.ISysExterLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service
public class SysExterLogApiServiceImpl extends ServiceImpl<ISysExterLogDao, SysExterLogApiEntity> implements ISysExterLogService {

    @Override
    public PageUtils queryPage(LogForm form ) {
        
    	QueryWrapper<SysExterLogApiEntity> query = new QueryWrapper<SysExterLogApiEntity>();
    	 
    	
    	query.likeRight(StringUtils.isNotBlank(form.getAppname()), "appname" , form.getAppname());
    	query.like(StringUtils.isNotBlank(form.getOperation()), "operation" , form.getOperation());
    	if(form.getEndTime() != null && form.getEndTime() > 0) {
    		query.le("create_date", new Date(form.getEndTime()));
    	}
    	if(form.getStartTime() != null && form.getStartTime() > 0) {
    		query.ge("create_date", new Date(form.getStartTime()));
    	}
    	
    	Integer pageIndex = form.getPageIndex();
    	Integer pageSize = form.getPageSize();
    	if(pageIndex == null || pageIndex <= 0) {
    		pageIndex = 1;
    	}
    	if(pageSize == null || pageSize <= 0 || pageSize >= 200) {
    		pageSize = 10;
    	}
    	
    	Query<SysExterLogApiEntity> pquery = new Query<>(pageIndex, pageSize/*, "create_date", false*/);
        
        IPage<SysExterLogApiEntity> page = this.page(
        		pquery.getPage(),query 
        		.orderByDesc("create_date")
        );

        return new PageUtils(page);
    }
}
